package com.wu.neihan.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.wu.neihan.main.UserMain;
import com.wu.neihan.model.User;
import com.wu.util.ConMysql;
import com.wu.util.DateUtil;
import com.wu.util.HttpUtil;
import com.wu.util.JsonUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GatherService {
	private static Logger logger = Logger.getLogger(GatherService.class);
	private ConMysql conMysql = new ConMysql();

	// 采集用户信息
	public  void caiUserInfo(String id) {
		// 检查id
		if (!conMysql.checkId("select id from mto_users where id='" + id + "' limit 1")) {
			// 获取采集链接
			String url = "http://isub.snssdk.com/neihan/user/profile/v2/?user_id=" + id;

			// 读取内容封装user
			String body = HttpUtil.sendGet(url);
			JSONObject bodyJs = JsonUtil.stringToJson(JsonUtil.stringToJson(body).getString("data"));
			if (bodyJs.size() > 1) {
				User user = new User(bodyJs.getString("large_avatar_url"), bodyJs.getString("avatar_url"),
						bodyJs.getString("name"), bodyJs.getString("city"), bodyJs.getInt("followers"),
						bodyJs.getInt("followings"), bodyJs.getInt("point"), bodyJs.getInt("ugc_count"),
						Integer.valueOf(bodyJs.getString("comment_count").trim()));
				
				// 存储采集
				// "created,email,last_login,mobile,password,status,username,name,avatar,updated,gender,role_id,source,active_email,comments,fans,favors,follows,posts,signature,region
				// "
				// System.out.println(bodyJs.getLong("create_time"));

				String sql = "INSERT INTO `mto_users` VALUES (" + bodyJs.getString("user_id") + ",'"
						+ DateUtil.getDate(bodyJs.getString("create_time") + "000", null) + "', '"
						+ bodyJs.getString("user_id") + "@qq.com', " + "'" + DateUtil.getDate(new Date(), null)
						+ "', null, '8FLF2EE7LSBJ8LIM6IIL261F8', '0', '" + user.getName().replace("'", "''") + "', '"
						+ bodyJs.getString("screen_name").replace("'", "''") + "', '" + bodyJs.getString("large_avatar_url") + "', null, '"
						+ bodyJs.getString("gender").replace("'", "''") + "', null, '0', '0', '" + user.getPinglun() + "', '"
						+ user.getFensi() + "', '" + bodyJs.getString("repin_count") + "','" + user.getGuanzhu()
						+ "', '" + user.getTougao() + "', '" + bodyJs.getString("description").replace("'", "''") + "', '"
						+ user.getRegion().replace("'", "''") + "');";
				// System.out.println(sql);
				try {
					if (conMysql.insertSql(sql) == 1) {
						logger.info("插入会员成功：" + bodyJs.getString("user_id") + "   " + user.getName());
						// GatherService.saveGather("插入会员成功：" +
						// bodyJs.getString("user_id") + " " + user.getName(),
						// 2);
					} else {
						logger.info("插入会员失败：" + bodyJs.getString("user_id") + "   " + user.getName());
						// GatherService.saveGather("插入会员失败：" +
						// bodyJs.getString("user_id") + " " + user.getName(),
						// 2);
					}
				} catch (Exception e) {
					logger.info("插入会员失败：" + sql);
					logger.info("插入会员失败：" + bodyJs.getString("user_id") + "   " + user.getName());
					e.printStackTrace();
				}
			} else {
				logger.info("会员信息不纯在：" + id);
				// GatherService.saveGather("会员信息不纯在：" + id, 2);
			}
		}
	}

	// 采集投稿信息
	public  void caiPostsInfo(String id, String max_time) {

		// 获取采集链接
		String url = "http://is.snssdk.com/2/essay/zone/user/posts/?screen_width=1080&user_id=" + id + "&max_time="
				+ max_time;

		// 读取内容封装
		String body = HttpUtil.sendGet(url);
		JSONObject bodyJs = JsonUtil.stringToJson(JsonUtil.stringToJson(body).getString("data"));
		if (bodyJs.size() > 1) {
			JSONArray bodyJa = JsonUtil.stringToJsonArry(bodyJs.getString("data"));
			for (int i = 0; i < bodyJa.size(); i++) {

				JSONObject bodyTemp;
				bodyTemp = JsonUtil.stringToJson(JsonUtil.stringToJson(bodyJa.get(i).toString()).getString("group"));

				// 通用内容采集
				String group = "1";
				max_time = bodyTemp.getString("create_time");
				String biaoqian = bodyTemp.getString("category_name");
				String idstr = bodyTemp.getString("id");
				String statusDesc = bodyTemp.getString("status_desc");
				String text = bodyTemp.getString("text");
				String content = bodyTemp.getString("content");
				int digg = bodyTemp.getInt("digg_count");
				int bury = bodyTemp.getInt("bury_count");
				int share = bodyTemp.getInt("share_count");
				int comment = bodyTemp.getInt("comment_count");
				String imageUrl = "";
				String videoImgUrl = "";
				String videoUrl = "";
				String width = "";
				String height = "";
				String privacy = "0";// 是否私密，后面将改为是否匿名
				// System.out.println(bodyTemp.toString());
				if (bodyTemp.getBoolean("is_anonymous")) {
					privacy = "1";
				}
				// System.out.println(biaoqian);
				// 图片采集
				// 大图格式例子
				// url=http://p3.pstatp.com/w540/11aa00031c961e2e8d0f.webp
				// url=http://pb2.pstatp.com/w540/11aa00031c961e2e8d0f.webp
				// url=http://pb3.pstatp.com/w540/11aa00031c961e2e8d0f.webp
				// 小图格式例子
				// url=http://p3.pstatp.com/w480/11aa00031c961e2e8d0f.webp
				// url=http://pb2.pstatp.com/w480/11aa00031c961e2e8d0f.webp
				// url=http://pb3.pstatp.com/w480/11aa00031c961e2e8d0f.webp
				if (bodyTemp.containsKey("large_image")) {
					JSONObject videoJson = JsonUtil.stringToJson(bodyTemp.getString("large_image"));
					group = "2";
					imageUrl = JsonUtil
							.stringToJson(JsonUtil.stringToJsonArry(videoJson.getString("url_list")).get(0).toString())
							.getString("url");
					width = videoJson.getString("r_width");
					// System.out.println(width);
					height = videoJson.getString("r_height");
					// System.out.println(imageUrl);
				}

				// 视频采集
				// 缩略图
				if (bodyTemp.containsKey("large_cover")) {
					videoImgUrl = JsonUtil.stringToJson(JsonUtil
							.stringToJsonArry(
									JsonUtil.stringToJson(bodyTemp.getString("large_cover")).getString("url_list"))
							.get(0).toString()).getString("url");
					// System.out.println(videoImgUrl);
				}
				// 视频链接
				// http://ic.snssdk.com/neihan/video/playback/?video_id=47bd2bb976fe470090373dbcae8151fa&quality=origin&line=0&is_gif=0
				// http://ic.snssdk.com/neihan/video/playback/?video_id=47bd2bb976fe470090373dbcae8151fa&quality=720p&line=0&is_gif=0
				// http://ic.snssdk.com/neihan/video/playback/?video_id=47bd2bb976fe470090373dbcae8151fa&quality=480p&line=0&is_gif=0
				// http://ic.snssdk.com/neihan/video/playback/?video_id=47bd2bb976fe470090373dbcae8151fa&quality=360p&line=0&is_gif=0
				if (bodyTemp.containsKey("origin_video")) {
					group = "3";
					JSONObject videoJson = JsonUtil.stringToJson(bodyTemp.getString("origin_video"));
					videoUrl = JsonUtil
							.stringToJson(JsonUtil.stringToJsonArry(videoJson.getString("url_list")).get(0).toString())
							.getString("url");
					// System.out.println(videoUrl);
					width = videoJson.getString("width");
					// System.out.println(width);
					height = videoJson.getString("height");
					// System.out.println(height);
				}

				String contentBody = content;
				// 存储采集
				if (!imageUrl.equals("")) {
					contentBody = "<img  src=" + imageUrl + "  width=" + width + " height=" + height + " />";
					// contentBody= "<iframe src=/getRes?url=" + imageUrl +
					// "
					// frameborder=0 scrolling=no width=" + width
					// + " height=" + height + " marginheight=0
					// marginwidth=0
					// ></iframe>";
				}
				// QueueService.putcommentQueue(idstr);
				// System.out.println(content);
				if (!conMysql.checkId("select id from mto_posts where id='" + idstr + "' limit 1")) {
					String sql = "INSERT INTO `mto_posts` VALUES ('" + idstr + "', '" + id + "', " + comment + ", '"
							+ contentBody.replace("'", "''") + "', '" + DateUtil.getDate(max_time + "000", null)
							+ "', 'ueditor', '0', '0', '" + group + "', '0', '0', null, '" + privacy + "', '0', '"
							+ content.replace("'", "''") + "', '" + biaoqian.replace("'", "''") + "', '" + text.replace("'", "''") + "', '0', '" + bury + "', '" + digg + "', '"
							+ share + "');";

					// System.out.println(sql);
					try {
						if (conMysql.insertSql(sql) == 1) {
							logger.info("插入段子成功：" + text);
							// GatherService.saveGather("插入段子成功：" + content, 3);
							// 放入带采集的评论队列库表mto_gather
							//saveGather(idstr, 3);
							//采集评论
							caiComtentsInfo(idstr,0);
							
							
							if (!videoUrl.equals("")) {
								sql = " INSERT INTO `mto_posts_attribute` VALUES ('" + idstr + "', '" + videoImgUrl
										+ "', '" + videoUrl + "','" + height + "','" + width + "');";

								// System.out.println(sql);
								try {
									if (conMysql.insertSql(sql) == 1) {
										logger.info("插入附件成功：" + content);
										// GatherService.saveGather("插入附件成功：" +
										// content, 3);
									} else {
										logger.info("插入附件失败：" + content);
										// GatherService.saveGather("插入附件失败：" +
										// content, 3);
									}
								} catch (Exception e) {
									logger.info("插入附件失败：" + sql);
									e.printStackTrace();
									logger.info("插入附件失败：" + content);
								}
							}

						} else {
							logger.info("插入段子失败：" + text);
							// GatherService.saveGather("插入段子失败：" + content, 3);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						logger.info("插入段子失败：" + sql);
						e.printStackTrace();
						logger.info("插入段子失败：" + text);
					}
				}
			}
			// System.out.println(bodyJs.getBoolean("has_more"));
			if (bodyJs.getBoolean("has_more")) {
				caiPostsInfo(id, max_time);
			}
		} else {
			logger.info("段子信息不纯在：" + id);
			// GatherService.saveGather("段子信息不纯在：" + id, 3);
		}

	}

	// 采集评论信息
	public  void caiComtentsInfo(String id, int offset) {
		// 获取采集链接
		String url = "http://isub.snssdk.com/neihan/comments/?group_id=" + id + "&offset=" + offset;

		// 读取内容封装
		String body = HttpUtil.sendGet(url);
		String res = "";
		JSONObject bodyJs = JsonUtil.stringToJson(JsonUtil.stringToJson(body).getString("data"));
		if (bodyJs.size() > 1) {
			
			JSONArray bodyJa = JsonUtil.stringToJsonArry(bodyJs.getString("recent_comments"));
			bodyJa.addAll(JsonUtil.stringToJsonArry(bodyJs.getString("top_comments")));
			for (int i = 0; i < bodyJa.size(); i++) {

				JSONObject bodyTemp;
				bodyTemp = JsonUtil.stringToJson(bodyJa.get(i).toString());

				// 采集评论内容
				String ids = bodyTemp.getString("id");
				String toid = bodyTemp.getString("group_id");
				String authorid = bodyTemp.getString("user_id");
				String content = bodyTemp.getString("text");
				String create_time = bodyTemp.getString("create_time");
				int digg = bodyTemp.getInt("digg_count");
				String pid = "0";
				// 判断是不是引用回复
				if (bodyTemp.containsKey("reply_comments")) {
					JSONArray bodyArrTemp = JsonUtil.stringToJsonArry(bodyTemp.getString("reply_comments"));
					pid = JsonUtil.stringToJson(bodyArrTemp.get(0).toString()).getString("id")+toid;
				}
				if (!conMysql.checkId("select id from mto_comments where id='" + ids +toid+ "' limit 1")) {

					String sql = " INSERT INTO `mto_comments` VALUES ('" + ids +toid+ "', '" + authorid + "', '" + content.replace("'", "''")
							+ "','" + DateUtil.getDate(create_time + "000", null) + "','" + pid + "','0','" + toid
							+ "','" + digg + "');";

					// System.out.println(sql);
					try {
						if (conMysql.insertSql(sql) == 1) {
							logger.info("插入评论成功：" + content);
							// res = res + "插入评论成功：" + content + "<br/>";

							
							caiUserInfo(authorid);//直接采集用户
							
							// 放入采集投稿的队列
							saveGather(authorid, 2);
							// QueueService.putUserQueue(authorid);
							// QueueService.putpostQueue(authorid);

						} else {
							logger.info("插入评论失败：" + content);
							// res = res + "插入评论失败：" + content + "<br/>";
						}
					} catch (Exception e) {
						logger.info("插入评论失败：" + sql);
						e.printStackTrace();
						logger.info("插入评论失败：" + content);
					}
				}
			}
			// if(!res.equals("")){GatherService.saveGather(res, 4);}
			if (JsonUtil.stringToJson(body).getBoolean("has_more")) {
				caiComtentsInfo(id, offset + 20);
			}
		} else {
			logger.info("评论信息不纯在：" + id);
			// GatherService.saveGather("评论信息不纯在：" + id, 4);
		}
	}

	public  void saveGather(String id, int type) {
		try {
			Date nowTime = new Date();
			// System.out.println(nowTime);
			SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// System.out.println(time.format(nowTime));

			// 保存会员信息
			// ConMysql.insertSql("update mto_gather set start_id='" + id +
			// "',last_date='" + time.format(nowTime)
			// + "' where type=" + type + " order by last_date limit 1");
			boolean isInsert=true;
			if(type==1&&conMysql.checkId("select id from mto_users where id='" + id + "' limit 1")){
				isInsert=false;
			}
			if(type==3&&conMysql.checkId("select to_id from mto_comments where to_id='" + id + "' limit 1")){
				isInsert=false;
			}
			
			if (isInsert) {

				conMysql.insertSql("insert into  mto_gather (start_id,type,last_date) values('" + id + "','" + type
						+ "','" + time.format(nowTime) + "');");
				// System.out.println("update mto_gather set start_id='" + id +
				// "',last_date='"+time.format(nowTime)+"' where type="+type+"
				// order
				// by last_date limit 1");
				if (type == 3) {
					logger.info("保存评论队列ID成功" + id);
				}
				if (type == 2) {
					logger.info("保存段子队列ID成功" + id);
				}
				if (type == 1) {
					logger.info("保存用户队列ID成功" + id);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.info("保存采集ID失败" + id);
		}
	}
}
